import React, { Component } from 'react'
import store from '../redux/store'
import {createIncrementAction,createReduceAction} from '../redux/count_action'

export default class Count extends Component {
  state = {carName: "奥迪"}
/*   componentDidMount(){
    //检测redux中状态的变化，只要变化，就调用render
    store.subscribe(() => {
      this.setState({})
    });
  } */
  reduce = ()=>{
    const {value} = this.selectNumber
    store.dispatch(createReduceAction(value*1))
  }
  increment = ()=>{
    const {value} = this.selectNumber
    store.dispatch(createIncrementAction(value*1))
  }
  incrementIfOdd = ()=>{
    const {value} = this.selectNumber
    const count = store.getState()
    if(count%2===1){
      store.dispatch(createIncrementAction(value*1))
    }
  }
  incrementAsync = ()=>{
    const {value} = this.selectNumber
    setTimeout(()=>{
      store.dispatch(createIncrementAction(value*1))
    },500)
  }
  
  render() {
    return <div>
        <h2>总数求和为：{store.getState()}</h2>&nbsp;
        <select ref={c => this.selectNumber = c}>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
        </select>&nbsp;
        <button onClick={this.reduce}>-</button>&nbsp;
        <button onClick={this.increment}>+</button>&nbsp;
        <button onClick={this.incrementIfOdd}>incrementIfOdd</button>&nbsp;
        <button onClick={this.incrementAsync}>incrementAsync</button>
    </div>;
  }
}
